package com.example.industry.dao.impl;

import com.example.industry.util.MySqlConnector;
import com.example.industry.enums.TaskStatus;
import com.example.industry.dao.TaskDao;
import com.example.industry.entity.Task;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

@Repository
public class TaskDaoImpl implements TaskDao {
    @Override
    public Task findById(Integer id) {
        Task task = new Task();
        MySqlConnector connector = new MySqlConnector();
        try {
            connector.connectDatabase();
            Connection conn = connector.getConnection();
            String sql = "select * from task where task_id = " + id;
            try (Statement stmt = conn.createStatement()) {
                // 获取task_id和description和state
                ResultSet resultSet = stmt.executeQuery(sql);
                if (resultSet.next()) {
                    task.setTaskId(resultSet.getInt("task_id"));
                    task.setIntroduction(resultSet.getString("introduction"));
                    if (resultSet.getString("status").equals("1")){
                        task.setStatus(TaskStatus.RUNNING);
                    }else task.setStatus(TaskStatus.DONE);
                } else return null;
                // 获取projectId
                String sql1 = "select * from project_task where task_id = " + id;
                ResultSet resultSet1 = stmt.executeQuery(sql1);
                if (resultSet1.next()){
                    task.setProjectId(resultSet1.getInt("project_id"));
                } else return null;
                // 获取任务指派者id
                String sql2 = "select * from task_assign where task_id = " + id;
                ResultSet resultSet2 = stmt.executeQuery(sql2);
                if (resultSet2.next()){
                    task.setAssignerID(resultSet2.getInt("user_id"));
                } else return null;
                // 获取任务接受者id
                String sql3 = "select * from task_accept where task_id = " + id;
                List<Integer> accepterId = new ArrayList<>();
                ResultSet resultSet3 = stmt.executeQuery(sql3);
                while (resultSet3.next()){
                    accepterId.add(resultSet3.getInt("user_id"));
                }
                task.setAccepterId(accepterId);
            }
            connector.closeConnection();
            return task;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    @Override
    public Boolean deleteById(Integer id) {
        if (findById(id) == null) return Boolean.FALSE;
        MySqlConnector connector = new MySqlConnector();
        try {
            connector.connectDatabase();
            Connection conn = connector.getConnection();
            String sql4 = "delete from task where task_id = " + id;
            try (Statement stmt = conn.createStatement()) {
                int result4 = stmt.executeUpdate(sql4);
                if (result4 <= 0) return Boolean.FALSE;
            }
            connector.closeConnection();
            return Boolean.TRUE;
        } catch (Exception e) {
            e.printStackTrace();
            return Boolean.FALSE;
        }
    }
    @Override
    public Boolean addTask(Task task) {
        if (task == null || task.getIntroduction() == null || task.getStatus() == null || task.getAssignerID() == null || task.getAccepterId() == null) return Boolean.FALSE;
        MySqlConnector connector = new MySqlConnector();
        int tempTaskId;
        try
        {
            connector.connectDatabase();
            Connection conn = connector.getConnection();
            int state = 0;
            if (task.getStatus() == TaskStatus.DONE)state=1;
            try (Statement stmt = conn.createStatement())
            {
                String sql1 = "insert into task "+"(introduction,status) " +
                    "values('"+ task.getIntroduction() + "'," + state + ")";
                if(stmt.executeUpdate(sql1,Statement.RETURN_GENERATED_KEYS) < 1)return Boolean.FALSE;
                ResultSet generatedKeys = stmt.getGeneratedKeys();
                if (generatedKeys != null) {
                    generatedKeys.next();
                    tempTaskId = generatedKeys.getInt(1);
                } else return Boolean.FALSE;
                String sql2 = "insert into project_task (project_id,task_id) " +
                        "values(" + task.getProjectId() + "," + tempTaskId + ")";
                if (stmt.executeUpdate(sql2) < 1) return Boolean.FALSE;

                String sql3 = "insert into task_assign (task_id,user_id) "+
                        "values(" + tempTaskId + "," + task.getAssignerID() + ")";
                if(stmt.executeUpdate(sql3) < 1) return Boolean.FALSE;

                for(Integer i : task.getAccepterId()){
                    String sql = "insert into task_accept (task_id,user_id) " +
                            "values (" + tempTaskId + "," + i + ")";
                    if(stmt.executeUpdate(sql) < 1) return Boolean.FALSE;
                }
            }
            connector.closeConnection();
            return Boolean.FALSE;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return Boolean.FALSE;
        }
    }
    @Override
    public Boolean update(Task task) {
        if (task == null || task.getIntroduction() == null || task.getStatus() == null) return Boolean.FALSE;
        MySqlConnector connector = new MySqlConnector();
        try
        {
            connector.connectDatabase();
            int state=0;
            if (task.getStatus() == TaskStatus.DONE) state=1;
            Connection conn = connector.getConnection();
            String sql = "update task set " + "introduction = '" + task.getIntroduction() + "',status = " + state +
                    " where task_id = " + task.getTaskId();
            try (Statement stmt = conn.createStatement())
            {
                int result = stmt.executeUpdate(sql);
                if (result <= 0) return Boolean.FALSE;
            }
            connector.closeConnection();
            return Boolean.TRUE;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return Boolean.FALSE;
        }
    }
}
